﻿
$('.fa-sort-up,.fa-sort-down').parents('th').css({ "cursor": "pointer" });

$('.fa-sort-up,.fa-sort-down').parents('th').on('click', function () {
    sort.call(this, $(this).attr('sort-column'));
});

function addPageSizeToUrl(url) {
    var result = url;
    if (document.getElementById('pagingData')) {
        var paging = JSON.parse(document.getElementById('pagingData').innerHTML.trim());
        if (paging.PageSize > 20) {
            result = utils.AddParamToUrl(result, "pageSize", paging.PageSize);
        }
    }

    return result;
}

function sort(col) {
    var order = "";
    if ($(this).find('.active').length > 0) {
        order = $(this).find('.active').hasClass('fa-sort-up') ? "desc" : "asc";
    }
    else {
        order = "asc";
    }
    if (col) {

        var url = window.location.href;
        url = utils.RemoveQueryParam(url, "pageIndex");
        url = utils.RemoveQueryParam(url, "pageSize");
        url = utils.AddParamToUrl(url, 'sort', col);
        url = utils.AddParamToUrl(url, 'order', order);
        url = addPageSizeToUrl(url);

        window.location.href = url;
    }
};